﻿using System;
using ChinaMobile;
using Fee.Orders.FormalWorkflow;
using Fee.Orders.PreWorkflow;
using Microsoft.Extensions.Logging;

namespace Fee.MingTong
{
    public class MingTongManager
    {
        private readonly ILogger<MingTongManager> _logger;
        private readonly MingTongWorkOrderManager _manager;

        public MingTongManager(MingTongWorkOrderManager manager, ILogger<MingTongManager> logger)
        {
            _manager = manager;
            _logger = logger;
        }

        public void PreOrderSubmit(PreDelegateOrder order)
        {
            try
            {
                var info = new MingTongProcessInfo
                {
                    OrderType = OrderType.PreOrder,
                    taskSN = order.ThirdPartyWorkflowId
                };
                //var vendorNames = new Dictionary<string, string>();
                foreach (var item in order.ConstructionTask.Details)
                    if (item.ContractType == ContractType.Design)
                    {
                        info.fieldMap.Add("设计单位", item.Contract.Vendor.Name);
                        info.fieldMap.Add("设备设计委托时间", order.FinishTime.Value.ToString("yyyy-MM-dd HH:mm:ss"));
                        info.fieldMap.Add("预委托处理时间", order.FinishTime.Value.ToString("yyyy-MM-dd HH:mm:ss"));
                        break;
                    }


                var result = _manager.PostTo(info);
                _logger.LogInformation(result.Code + ":" + result.Desc);

                if (result.Code != "0") throw new Exception("通知名通失败");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "通知名通失败");
                throw ex;
            }
        }

        public void FormalOrderSubmit(FormalOrder order)
        {
            try
            {
                var info = new MingTongProcessInfo
                {
                    OrderType = OrderType.PreOrder,
                    taskSN = order.ThirdPartyWorkflowId
                };
                //正式委托字段：施工单位、监理单位、正式委托时间。


                foreach (var item in order.ConstructionTask.Details)
                    if (item.ConstructionType == ConstructionType.Equipment)
                    {
                        if (item.ContractType == ContractType.Supervisor)
                        {
                            info.fieldMap.Add("监理单位", item.Contract.Vendor.Name);
                            continue;
                        }

                        if (item.ContractType == ContractType.Construction)
                        {
                            info.fieldMap.Add("施工单位", item.Contract.Vendor.Name);
                        }
                    }


                info.fieldMap.Add("正式委托时间", order.FinishTime.Value.ToString("yyyy-MM-dd HH:mm:ss"));
                var result = _manager.PostTo(info);

                _logger.LogInformation(result.Desc);

                if (result.Code != "0") throw new Exception("通知名通失败");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "通知名通失败");
                throw;
            }
        }
    }
}